﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-14
Description:MenuList.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using ExtAspNet;
using FTchina.SearchQuery;
using FTchina.Library;
using FTchina;
using FTchina.Logic;
using FTchina.DAL;

public partial class MenuList : System.Web.UI.Page
{

    string TableName = "MenuList";//表名
    static string SqlWhere = " 1=1 ";//过滤条件
    string GetCountSql = "select count(id)  from MenuList where " + SqlWhere;
    string SqlTable = "select *  from MenuList where " + SqlWhere + "  order by id desc ";
    string ReturnPage = "MenuList.aspx";
    string SearchFilter = " and ( id like '%{0}%'  )";//默认搜索id，其他字段按需添加

    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化模型
    string WindowsPage = "MenuListaction.aspx";//弹出窗口的页面地址

    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        if (!IsPostBack)
        {
            btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference(WindowsPage, "新增");
            if (qm.WorkPage != ReturnPage)
            {
                qm.TableName = TableName;
                qm.GetCountSql = GetCountSql;
                qm.GetTableSql = SqlTable;
                qm.ReturnPage = ReturnPage;
                qm.SqlWhere = SqlWhere;
                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGrid(qm);
        }
    }


    #region 绑定列表
    private void BindGrid(QueryModel qm)
    {
        BindGrid(qm, false);
    }

    /// <summary>
    /// 绑定表格
    /// </summary>
    private void BindGrid(QueryModel qm, bool IsToExcel)
    {
        DataTable dt=MenuListLogic.getTreeDataTable();

        if (IsToExcel)
        {//如果是导出Excel
            FTchina.OfficeHelper.ToExcel.DataTableToExcel(dt);
            return;
        }


        //每页条数
        Grid1.PageSize = 200;
        //总数
        Grid1.RecordCount = Convert.ToInt32(DBHelper.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
        //分页数据源
        Grid1.DataSource = dt;
        Grid1.DataBind();

        if (qm.Key1.Length > 0)
        {
            ttbSearch.Text = qm.Key1;
            ttbSearch.ShowTrigger1 = true;
        }

    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid(qm);
    }
    /// <summary>
    /// 删除按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int row in Grid1.SelectedRowIndexArray)
        {
            sb.Append(Grid1.DataKeys[row][0].ToString());
            sb.Append(",");
        }
        foreach (string ids in sb.ToString().TrimEnd(',').Split(','))
        {
            AlertLogic.ShowResult(MenuListLogic.DeleteMenuItem(ids));
           // FTchina.BLL.UsergroupManager.DeleteUsergroupByid(Convert.ToInt32(ids));
        }
        BindGrid(qm);
        Grid1.SelectedRowIndexArray = new int[] { };//去除默认的选项
    }


    /// <summary>
    /// 弹出窗口返回的处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Window1_Close(object sender, EventArgs e)
    {
        BindGrid(qm);
    }
    #endregion

    #region 搜索过滤
    /// <summary>
    /// 点击搜索上的关闭按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = false;
        ttbSearch.Text = String.Empty;//清空搜索字符串
        qm.Key1 = String.Empty;//清空搜索条件
        qm.GetCountSql = GetCountSql;
        qm.SqlWhere = SqlWhere;
        BindGrid(qm);
    }
    /// <summary>
    /// 点击搜索按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = true;//显示关闭搜索按钮
        string SqlWhere = " 1=1  ";//过滤条件，清空其他的过滤条件
        if (ttbSearch.Text.Trim().Length > 0)
        {
            qm.Key1 = ttbSearch.Text.Trim();
            SqlWhere += string.Format(SearchFilter, RequestHelper.SafeRequest(qm.Key1));
        }
        else
        {
            qm.Key1 = "";
        }
        string GetCountSql = "select count(id)  from MenuList where " + SqlWhere;
        qm.GetCountSql = GetCountSql;//总数
        qm.SqlWhere = SqlWhere;

        BindGrid(qm);
    }


    #endregion


    #region 导出Excel
    /// <summary>
    /// 带过滤条件的导出，不分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_click(object sender, EventArgs e)
    {
        BindGrid(qm, true);
    }
    #endregion




    #region 获取权限名称
    /// <summary>
    /// 通过权限名称获取权限ID
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    protected String GetPowerIDByName(string name)
    {
        string sql = "select id from usergroup where groupname='" + name.Trim() + "'";
        //basic.Log(sql);
        return DBHelper.GetDataTable(sql).Rows[0][0].ToString().Trim();
    }
    /// <summary>
    /// 通过权限ID获取权限名称
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    protected String GetPowerNameById(string id)
    {
        string sql = "select groupname from usergroup where id=" + id + "";
        // basic.Log(sql);
        return DBHelper.GetDataTable(sql).Rows[0][0].ToString().Trim();
    }


    /// <summary>
    /// 通过ids 获取 powernames
    /// </summary>
    /// <param name="ids">1,2,3</param>
    /// <returns>系统管理员,普通用户,审核组</returns>
    protected String GetPowerNamesByIds(string ids)
    {
        string tmp = string.Empty;
        if (ids.Contains(","))
        {
            foreach (string str in ids.Split(','))
            {
                if (str.Trim().Length > 0)
                {
                    tmp += GetPowerNameById(str.Trim()) + ",";
                }
            }
        }
        else
        {
            if (ids.Trim().Length > 0)
            {
                tmp = GetPowerNameById(ids);
            }
        }
        if (tmp.EndsWith(","))
        {
            tmp = tmp.Substring(0, tmp.Length - 1);
        }

        return tmp;

    }
    #endregion
}






